package Map;

import java.util.*;

/**
 * @author 王艺锡
 * @version 1.0
 */
public class MapMethod {
    @SuppressWarnings({"all"})
    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("王艺锡","ly");
        map.put(null,"1");
        map.put("num3","2");
        map.put("num4","3");
        map.put("num5","4");
        map.put("num6","5");
        map.put("num7","6");

        //1.remove删除
        map.remove(null);
        System.out.println(map);

        //2.get根据k值获得v值
        Object wyx = map.get("王艺锡");
        System.out.println(wyx);

        //3.size获取元素个数
        System.out.println(map.size());

        //4.isEmpty
        System.out.println(map.isEmpty());//F

        //5.clear清空
        //map.clear();

        //6.containsKey查找键是否存在
        System.out.println(map.containsKey ("王艺锡"));


        //遍历方法
        //1.先取出所有的key，通过key在取出所有的value
        Set set = map.keySet();//取出所有的k
        System.out.println("------第1种方式--------");
        for (Object key :set) {
            System.out.println(key + "_" + map.get(key));
        }
        System.out.println("------第2种方式--------");
        Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            Object key = iterator.next();
            System.out.println(key + "_" + map.get(key));
        }


        System.out.println("====================");
        //2.把所有v取出
        Collection values = map.values();
        for (Object v :values) {
            System.out.print(v + "  ");
        }

        System.out.println();


        System.out.println("====================");
        //3.通过EntrySet来取出k-v
        Set entrySet = map.entrySet();//此时取出的entrySet的类型是HashMap$EntrySet
        //System.out.println(entrySet.getClass());
        System.out.println("------第3种方式--------");
        for (Object entry :entrySet) {
            Map.Entry m = (Map.Entry) entry;
            System.out.println(m.getKey() + "_" + m.getValue());
        }

        System.out.println("------第4种方式--------");//使用EntrySet的迭代器
        Iterator iterator1 = entrySet.iterator();
        while (iterator1.hasNext()) {
            Object entry = iterator1.next();//此时迭代器取出entry的类型是HashMap$Node
            //System.out.println(entry.getClass());
            Map.Entry m = (Map.Entry) entry;
            System.out.println(m.getKey() + "_" + m.getValue());
        }
    }
}
